import axios from 'axios';
import {Message} from 'element-ui';
import {getToken} from "@/utils/storage";


const service = axios.create({
  baseURL: process.env.NODE_ENV === 'production' ? '' : '/api',
  timeout: 5000
});

//service  : 是什么? axios 实例
service.interceptors.request.use(config => {
  config.headers['token'] = getToken();
  // 不能省略
  return config;
},error=>{
  Message({
    message: '请检查网络',
    type:'error',
    duration: 2000
  });
  return Promise.reject(error);
});

service.interceptors.response.use(response => {


  // 写接口  1:成功  -1:失败
  return response.data;

},error=>{
  Message({
    message: '请求失败',// 404 405 500 401:未授权  403:进制访问
    type: 'error',
    duration: 2000
  });
  console.error(error);
  return Promise.reject(error);
});

export default service;


